<!DOCTYPE html>












  


<html class="theme-next gemini use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">


























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.0.1">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.0.1">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.0.1">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.0.1">


  <link rel="mask-icon" href="/images/logo.svg?v=7.0.1" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.0.1',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false,"dimmer":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="在Windows中善用批处理可以简化很多重复工作前言批处理文件（batch file）包含一系列 DOS命令，通常用于自动执行重复性任务。用户只需双击批处理文件便可执行任务，而无需重复输入相同指令。编写批处理文件非常简单，但难点在于确保一切按顺序执行。编写严谨的批处理文件可以极大程度地节省时间，在应对重复性工作时尤其有效。  在Windows中善用批处理可以简化很多重复工作  更新记录2016年0">
<meta name="keywords" content="bat,批处理">
<meta property="og:type" content="article">
<meta property="og:title" content="Windows批处理(cmd&#x2F;bat)常用命令小结">
<meta property="og:url" content="https://xsin.gitee.io/2018/12/25/Windows批处理(cmdbat)常用命令小结/index.html">
<meta property="og:site_name" content="技术存档笔记咯">
<meta property="og:description" content="在Windows中善用批处理可以简化很多重复工作前言批处理文件（batch file）包含一系列 DOS命令，通常用于自动执行重复性任务。用户只需双击批处理文件便可执行任务，而无需重复输入相同指令。编写批处理文件非常简单，但难点在于确保一切按顺序执行。编写严谨的批处理文件可以极大程度地节省时间，在应对重复性工作时尤其有效。  在Windows中善用批处理可以简化很多重复工作  更新记录2016年0">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-01-15T03:26:51.634Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Windows批处理(cmd&#x2F;bat)常用命令小结">
<meta name="twitter:description" content="在Windows中善用批处理可以简化很多重复工作前言批处理文件（batch file）包含一系列 DOS命令，通常用于自动执行重复性任务。用户只需双击批处理文件便可执行任务，而无需重复输入相同指令。编写批处理文件非常简单，但难点在于确保一切按顺序执行。编写严谨的批处理文件可以极大程度地节省时间，在应对重复性工作时尤其有效。  在Windows中善用批处理可以简化很多重复工作  更新记录2016年0">






  <link rel="canonical" href="https://xsin.gitee.io/2018/12/25/Windows批处理(cmdbat)常用命令小结/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Windows批处理(cmd/bat)常用命令小结 | 技术存档笔记咯</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript><!-- hexo-inject:begin --><!-- hexo-inject:end -->

</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <!-- hexo-inject:begin --><!-- hexo-inject:end --><div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">技术存档笔记咯</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <p class="site-subtitle">人生很南，也很靠北，所以才会想东想西...</p>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  
    <div class="reading-progress-bar"></div>
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://xsin.gitee.io/2018/12/25/Windows批处理(cmdbat)常用命令小结/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="xsin">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="技术存档笔记咯">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Windows批处理(cmd/bat)常用命令小结

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-12-25 16:22:32" itemprop="dateCreated datePublished" datetime="2018-12-25T16:22:32+08:00">2018-12-25</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-01-15 11:26:51" itemprop="dateModified" datetime="2019-01-15T11:26:51+08:00">2019-01-15</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing"><a href="/categories/学习/" itemprop="url" rel="index"><span itemprop="name">学习</span></a></span>

                
                
                  ，
                
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing"><a href="/categories/学习/批处理/" itemprop="url" rel="index"><span itemprop="name">批处理</span></a></span>

                
                
              
            </span>
          

          
            
            
              
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
            
                <span class="post-meta-item-text">评论数：</span>
                <a href="/2018/12/25/Windows批处理(cmdbat)常用命令小结/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2018/12/25/Windows批处理(cmdbat)常用命令小结/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="在Windows中善用批处理可以简化很多重复工作"><a href="#在Windows中善用批处理可以简化很多重复工作" class="headerlink" title="在Windows中善用批处理可以简化很多重复工作"></a>在Windows中善用批处理可以简化很多重复工作</h2><h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>批处理文件（batch file）包含一系列 DOS命令，通常用于自动执行重复性任务。用户只需双击批处理文件便可执行任务，而无需重复输入相同指令。编写批处理文件非常简单，但难点在于确保一切按顺序执行。编写严谨的批处理文件可以极大程度地节省时间，在应对重复性工作时尤其有效。</p>
<blockquote>
<p>在Windows中善用批处理可以简化很多重复工作</p>
</blockquote>
<h2 id="更新记录"><a href="#更新记录" class="headerlink" title="更新记录"></a>更新记录</h2><p>2016年03月29日 - 初稿</p>
<p>阅读原文 - Windows批处理(cmd/bat)常用命令小结</p>
<p><strong>扩展阅读</strong></p>
<p>如何编写批处理文件 - wikiHow - 您可以编辑的指导手册编写批处理文件<br>批处理常用命令总结 - 批处理常用命令总结</p>
<h2 id="什么是批处理"><a href="#什么是批处理" class="headerlink" title="什么是批处理"></a>什么是批处理</h2><p>批处理(Batch)，也称为批处理脚本。顾名思义，批处理就是对某对象进行批量的处理。批处理文件的扩展名为bat。</p>
<p>目前比较常见 的批处理包含两类：DOS批处理和PS批处理。PS批处理是基于强大的图片编辑软件Photoshop的，用来批量处理图片的脚本；而DOS批处理则是基于DOS命令的，用来自动地批量地执行DOS命令以实现特定操作的脚本。这里要讲的就是DOS批处理。</p>
<p>批处理是一种简化的脚本语言，它应用于DOS和Windows系统中，它是由DOS或者Windows系统内嵌的命令解释器（通常是<a href="http://COMMAND.COM或者CMD.EXE）解释运行。类似于Unix中的Shell脚本。批处理文件具有.bat或者.cmd的扩展名，其最简单的例子，是逐行书写在命令行中会用到的各种命令。更复杂的情况，需要使用if，for，goto等命令控制程序的运行过程，如同C，Basic等中高级语言一样。如果需要实现更复杂的应用，利用外部程序是必要的，这包括系统本身提供的外部命令和第三方提供的工具或者软件。" target="_blank" rel="noopener">http://COMMAND.COM或者CMD.EXE）解释运行。类似于Unix中的Shell脚本。批处理文件具有.bat或者.cmd的扩展名，其最简单的例子，是逐行书写在命令行中会用到的各种命令。更复杂的情况，需要使用if，for，goto等命令控制程序的运行过程，如同C，Basic等中高级语言一样。如果需要实现更复杂的应用，利用外部程序是必要的，这包括系统本身提供的外部命令和第三方提供的工具或者软件。</a></p>
<p>批处理文件，或称为批处理程序，是由一条条的DOS命令组成的普通文本文件，可以用记事本直接编辑或用DOS命令创建，也可以用DOS下的文本编辑器Edit.exe来编辑。在“命令提示”下键入批处理文件的名称，或者双击该批处理文件，系统就会调用Cmd.exe运行该批处理程序。一般情况下，每条命令占据一行；当然也可以将多条命令用特定符号（如：&amp;、&amp;&amp;、|、||等）分隔后写入同一行中；还有的情况就是像if、for等较高级的命令则要占据几行甚至几十几百行的空间。</p>
<p>系统在解释运行批处理程序时，首先扫描整个批处理程序，然后从第一行代码开始向下逐句执行所有的命令，直至程序结尾或遇见exit命令或出错意外退出。</p>
<h2 id="批处理命令简介"><a href="#批处理命令简介" class="headerlink" title="批处理命令简介"></a>批处理命令简介</h2><ul>
<li>echo</li>
<li>rem</li>
<li>pause</li>
<li>call</li>
<li>start</li>
<li>goto</li>
<li>set</li>
</ul>
<p>批处理常用命令总结</p>
<h2 id="批处理符号简介"><a href="#批处理符号简介" class="headerlink" title="批处理符号简介"></a>批处理符号简介</h2><ul>
<li>回显屏蔽 @</li>
<li>重定向1 &gt;与&gt;&gt;</li>
<li>重定向2 &lt;</li>
<li>管道符号 |</li>
<li>转义符 ^</li>
<li>逻辑命令符包括：&amp;、&amp;&amp;、||</li>
</ul>
<p>批处理常用命令总结</p>
<h2 id="常用DOS命令"><a href="#常用DOS命令" class="headerlink" title="常用DOS命令"></a>常用DOS命令</h2><p><strong>文件夹管理</strong></p>
<ul>
<li>cd 显示当前目录名或改变当前目录。</li>
<li>md 创建目录。</li>
<li>rd 删除一个目录。</li>
<li>dir 显示目录中的文件和子目录列表。</li>
<li>tree 以图形显示驱动器或路径的文件夹结构。</li>
<li>path 为可执行文件显示或设置一个搜索路径。</li>
<li>xcopy 复制文件和目录树。</li>
</ul>
<p><strong>文件管理</strong></p>
<ul>
<li>type 显示文本文件的内容。</li>
<li>copy 将一份或多份文件复制到另一个位置。</li>
<li>del 删除一个或数个文件。</li>
<li>move 移动文件并重命名文件和目录。(Windows XP Home Edition中没有)</li>
<li>ren 重命名文件。</li>
<li>replace 替换文件。</li>
<li>attrib 显示或更改文件属性。</li>
<li>find 搜索字符串。</li>
<li>fc 比较两个文件或两个文件集并显示它们之间的不同</li>
</ul>
<p><strong>网络命令</strong></p>
<ul>
<li>ping 进行网络连接测试、名称解析</li>
<li>ftp 文件传输</li>
<li>net 网络命令集及用户管理</li>
<li>telnet 远程登陆</li>
<li>ipconfig显示、修改TCP/IP设置</li>
<li>msg 给用户发送消息</li>
<li>arp 显示、修改局域网的IP地址-物理地址映射列表</li>
</ul>
<p><strong>系统管理</strong></p>
<ul>
<li>at 安排在特定日期和时间运行命令和程序</li>
<li>shutdown立即或定时关机或重启</li>
<li>tskill 结束进程</li>
<li>taskkill结束进程(比tskill高级，但WinXPHome版中无该命令)</li>
<li>tasklist显示进程列表(Windows XP Home Edition中没有)</li>
<li>sc 系统服务设置与控制</li>
<li>reg 注册表控制台工具</li>
<li>powercfg控制系统上的电源设置</li>
</ul>
<blockquote>
<p>对于以上列出的所有命令，在cmd中输入命令+/?即可查看该命令的帮助信息。如find /?</p>
</blockquote>
<h2 id="Windows-Batch-常用命令"><a href="#Windows-Batch-常用命令" class="headerlink" title="Windows Batch 常用命令"></a>Windows Batch 常用命令</h2><figure class="highlight bat"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> <span class="built_in">echo</span> 和 @</span><br><span class="line">回显命令</span><br><span class="line">@ #关闭单行回显</span><br><span class="line"><span class="built_in">echo</span> off #从下一行开始关闭回显</span><br><span class="line">@<span class="built_in">echo</span> off #从本行开始关闭回显。一般批处理第一行都是这个</span><br><span class="line"><span class="built_in">echo</span> on #从下一行开始打开回显</span><br><span class="line"><span class="built_in">echo</span> #显示当前是 <span class="built_in">echo</span> off 状态还是 <span class="built_in">echo</span> on 状态</span><br><span class="line"><span class="built_in">echo</span>. #输出一个”回车换行”，空白行</span><br><span class="line">#(同<span class="built_in">echo</span>, <span class="built_in">echo</span>; <span class="built_in">echo</span>+ <span class="built_in">echo</span>[ <span class="built_in">echo</span>] <span class="built_in">echo</span>/ <span class="built_in">echo</span>)</span><br><span class="line"></span><br><span class="line"><span class="number">2</span> <span class="keyword">errorlevel</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">%errorlevel%</span></span><br><span class="line">每个命令运行结束，可以用这个命令行格式查看返回码</span><br><span class="line">默认值为<span class="number">0</span>，一般命令执行出错会设 <span class="keyword">errorlevel</span> 为<span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="number">3</span> <span class="built_in">dir</span></span><br><span class="line">显示文件夹内容</span><br><span class="line"><span class="built_in">dir</span> #显示当前目录中的文件和子目录</span><br><span class="line"><span class="built_in">dir</span> /a #显示当前目录中的文件和子目录，包括隐藏文件和系统文件</span><br><span class="line"><span class="built_in">dir</span> c: /a:d #显示 C 盘当前目录中的目录</span><br><span class="line"><span class="built_in">dir</span> c: /a:-d #显示 C 盘根目录中的文件</span><br><span class="line"><span class="built_in">dir</span> c: /b/p #/b只显示文件名，/p分页显示</span><br><span class="line"><span class="built_in">dir</span> *.exe /s #显示当前目录和子目录里所有的.exe文件</span><br><span class="line"></span><br><span class="line"><span class="number">4</span> <span class="built_in">cd</span></span><br><span class="line">切换目录</span><br><span class="line"><span class="built_in">cd</span> #进入根目录</span><br><span class="line"><span class="built_in">cd</span> #显示当前目录</span><br><span class="line"><span class="built_in">cd</span> /d d:sdk #可以同时更改盘符和目录</span><br><span class="line"></span><br><span class="line"><span class="number">5</span> <span class="built_in">md</span></span><br><span class="line">创建目录</span><br><span class="line"><span class="built_in">md</span> d:abc #如果 d:a 不存在，将会自动创建中级目录</span><br><span class="line">#如果命令扩展名被停用，则需要键入 <span class="built_in">mkdir</span> abc。</span><br><span class="line"></span><br><span class="line"><span class="number">6</span> <span class="built_in">rd</span></span><br><span class="line">删除目录</span><br><span class="line"><span class="built_in">rd</span> abc #删除当前目录里的 abc 子目录，要求为空目录</span><br><span class="line"><span class="built_in">rd</span> /s/q d:temp #删除 d:temp 文件夹及其子文件夹和文件，/q安静模式</span><br><span class="line"></span><br><span class="line"><span class="number">7</span> <span class="built_in">del</span></span><br><span class="line">删除文件</span><br><span class="line"><span class="built_in">del</span> d:test.txt #删除指定文件，不能是隐藏、系统、只读文件</span><br><span class="line"><span class="built_in">del</span> /q/a/f d:temp*.*</span><br><span class="line">删除 d:temp 文件夹里面的所有文件，包括隐藏、只读、系统文件，不包括子目录</span><br><span class="line"><span class="built_in">del</span> /q/a/f/s d:temp*.*</span><br><span class="line">删除 d:temp 及子文件夹里面的所有文件，包括隐藏、只读、系统文件，不包括子目录</span><br><span class="line"></span><br><span class="line"><span class="number">8</span> <span class="built_in">ren</span></span><br><span class="line">重命名命令</span><br><span class="line"><span class="built_in">ren</span> d:temp tmp #支持对文件夹的重命名</span><br><span class="line"></span><br><span class="line"><span class="number">9</span> <span class="built_in">cls</span></span><br><span class="line">清屏</span><br><span class="line"></span><br><span class="line"><span class="number">10</span> <span class="built_in">type</span></span><br><span class="line">显示文件内容</span><br><span class="line"><span class="built_in">type</span> c:boot.ini #显示指定文件的内容，程序文件一般会显示乱码</span><br><span class="line"><span class="built_in">type</span> *.txt #显示当前目录里所有.txt文件的内容</span><br><span class="line"></span><br><span class="line"><span class="number">11</span> <span class="built_in">copy</span></span><br><span class="line">拷贝文件</span><br><span class="line"><span class="built_in">copy</span> c:test.txt d:test.bak</span><br><span class="line">复制 c:test.txt 文件到 d: ，并重命名为 test.bak</span><br><span class="line"><span class="built_in">copy</span> <span class="built_in">con</span> test.txt</span><br><span class="line">从屏幕上等待输入，按 Ctrl+Z 结束输入，输入内容存为test.txt文件</span><br><span class="line"><span class="built_in">con</span>代表屏幕，<span class="built_in">prn</span>代表打印机，<span class="built_in">nul</span>代表空设备</span><br><span class="line"><span class="built_in">copy</span> <span class="number">1</span>.txt + <span class="number">2</span>.txt <span class="number">3</span>.txt</span><br><span class="line">合并 <span class="number">1</span>.txt 和 <span class="number">2</span>.txt 的内容，保存为 <span class="number">3</span>.txt 文件</span><br><span class="line">如果不指定 <span class="number">3</span>.txt ，则保存到 <span class="number">1</span>.txt</span><br><span class="line"><span class="built_in">copy</span> test.txt +</span><br><span class="line">复制文件到自己，实际上是修改了文件日期</span><br><span class="line"></span><br><span class="line"><span class="number">12</span> <span class="built_in">title</span></span><br><span class="line">设置<span class="built_in">cmd</span>窗口的标题</span><br><span class="line"><span class="built_in">title</span> 新标题 #可以看到<span class="built_in">cmd</span>窗口的标题栏变了</span><br><span class="line"></span><br><span class="line"><span class="number">13</span> <span class="built_in">ver</span></span><br><span class="line">显示系统版本</span><br><span class="line"></span><br><span class="line"><span class="number">14</span> <span class="built_in">label</span> 和 <span class="built_in">vol</span></span><br><span class="line">设置卷标</span><br><span class="line"><span class="built_in">vol</span> #显示卷标</span><br><span class="line"><span class="built_in">label</span> #显示卷标，同时提示输入新卷标</span><br><span class="line"><span class="built_in">label</span> c:system #设置C盘的卷标为 system</span><br><span class="line"></span><br><span class="line"><span class="number">15</span> <span class="built_in">pause</span></span><br><span class="line">暂停命令</span><br><span class="line"></span><br><span class="line"><span class="number">16</span> <span class="built_in">rem</span> 和 ::</span><br><span class="line">注释命令</span><br><span class="line">注释行不执行操作</span><br><span class="line"></span><br><span class="line"><span class="number">17</span> <span class="built_in">date</span> 和 <span class="built_in">time</span></span><br><span class="line">日期和时间</span><br><span class="line"><span class="built_in">date</span> #显示当前日期，并提示输入新日期，按"回车"略过输入</span><br><span class="line"><span class="built_in">date</span>/t #只显示当前日期，不提示输入新日期</span><br><span class="line"><span class="built_in">time</span> #显示当前时间，并提示输入新时间，按"回车"略过输入</span><br><span class="line"><span class="built_in">time</span>/t #只显示当前时间，不提示输入新时间</span><br><span class="line"></span><br><span class="line"><span class="number">18</span> <span class="keyword">goto</span> 和 :</span><br><span class="line">跳转命令</span><br><span class="line">:<span class="built_in">label</span> #行首为:表示该行是标签行，标签行不执行操作</span><br><span class="line"><span class="function">goto label #跳转到指定的标签那一行</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">19 <span class="title">find</span> (外部命令)</span></span><br><span class="line"><span class="function">查找命令</span></span><br><span class="line"><span class="function"><span class="title">find</span> "<span class="title">abc</span>" <span class="title">c:test</span>.<span class="title">txt</span></span></span><br><span class="line"><span class="function">在 <span class="title">c:test</span>.<span class="title">txt</span> 文件里查找含 <span class="title">abc</span> 字符串的行</span></span><br><span class="line"><span class="function">如果找不到，将设 <span class="title">errorlevel</span> 返回码为1</span></span><br><span class="line"><span class="function"><span class="title">find</span> /<span class="title">i</span> “<span class="title">abc</span>” <span class="title">c:test</span>.<span class="title">txt</span></span></span><br><span class="line"><span class="function">查找含 <span class="title">abc</span> 的行，忽略大小写</span></span><br><span class="line"><span class="function"><span class="title">find</span> /<span class="title">c</span> "<span class="title">abc</span>" <span class="title">c:test</span>.<span class="title">txt</span></span></span><br><span class="line"><span class="function">显示含 <span class="title">abc</span> 的行的行数</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">20 <span class="title">more</span> (外部命令)</span></span><br><span class="line"><span class="function">逐屏显示</span></span><br><span class="line"><span class="function"><span class="title">more</span> <span class="title">c:test</span>.<span class="title">txt</span> #逐屏显示 <span class="title">c:test</span>.<span class="title">txt</span> 的文件内容</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">21 <span class="title">tree</span></span></span><br><span class="line"><span class="function">显示目录结构</span></span><br><span class="line"><span class="function"><span class="title">tree</span> <span class="title">d</span>: #显示<span class="title">D</span>盘的文件目录结构</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">22 &amp;</span></span><br><span class="line"><span class="function">顺序执行多条命令，而不管命令是否执行成功</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">23 &amp;&amp;</span></span><br><span class="line"><span class="function">顺序执行多条命令，当碰到执行出错的命令后将不执行后面的命令</span></span><br><span class="line"><span class="function"><span class="title">find</span> "<span class="title">ok</span>" <span class="title">c:test</span>.<span class="title">txt</span> &amp;&amp; <span class="title">echo</span> 成功</span></span><br><span class="line"><span class="function">如果找到了"<span class="title">ok</span>"字样，就显示"成功"，找不到就不显示</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">24 ||</span></span><br><span class="line"><span class="function">顺序执行多条命令，当碰到执行正确的命令后将不执行后面的命令</span></span><br><span class="line"><span class="function"><span class="title">find</span> "<span class="title">ok</span>" <span class="title">c:test</span>.<span class="title">txt</span> || <span class="title">echo</span> 不成功</span></span><br><span class="line"><span class="function">如果找不到"<span class="title">ok</span>"字样，就显示"不成功"，找到了就不显示</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">25 |</span></span><br><span class="line"><span class="function">管道命令</span></span><br><span class="line"><span class="function"><span class="title">dir</span> *.* /<span class="title">s</span>/<span class="title">a</span> | <span class="title">find</span> /<span class="title">c</span> ".<span class="title">exe</span>"</span></span><br><span class="line"><span class="function">管道命令表示先执行 <span class="title">dir</span> 命令，对其输出的结果执行后面的 <span class="title">find</span> 命令</span></span><br><span class="line"><span class="function">该命令行结果：输出当前文件夹及所有子文件夹里的.<span class="title">exe</span>文件的个数</span></span><br><span class="line"><span class="function"><span class="title">type</span> <span class="title">c:test</span>.<span class="title">txt</span>|<span class="title">more</span></span></span><br><span class="line"><span class="function">这个和 <span class="title">more</span> <span class="title">c:test</span>.<span class="title">txt</span> 的效果是一样的</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">26 &gt; 和 &gt;&gt;</span></span><br><span class="line"><span class="function">输出重定向命令</span></span><br><span class="line"><span class="function">&gt; 清除文件中原有的内容后再写入</span></span><br><span class="line"><span class="function">&gt;&gt; 追加内容到文件末尾，而不会清除原有的内容</span></span><br><span class="line"><span class="function">主要将本来显示在屏幕上的内容输出到指定文件中</span></span><br><span class="line"><span class="function">指定文件如果不存在，则自动生成该文件</span></span><br><span class="line"><span class="function"><span class="title">type</span> <span class="title">c:test</span>.<span class="title">txt</span> &gt;<span class="title">prn</span></span></span><br><span class="line"><span class="function">屏幕上不显示文件内容，转向输出到打印机</span></span><br><span class="line"><span class="function"><span class="title">echo</span> <span class="title">hello</span> <span class="title">world</span>&gt;<span class="title">con</span></span></span><br><span class="line"><span class="function">在屏幕上显示<span class="title">hello</span> <span class="title">world</span>，实际上所有输出都是默认 &gt;<span class="title">con</span> 的</span></span><br><span class="line"><span class="function"><span class="title">copy</span> <span class="title">c:test</span>.<span class="title">txt</span> <span class="title">f</span>: &gt;<span class="title">nul</span></span></span><br><span class="line"><span class="function">拷贝文件，并且不显示"文件复制成功"的提示信息，但如果<span class="title">f</span>盘不存在，还是会显示出错信息</span></span><br><span class="line"><span class="function"><span class="title">copy</span> <span class="title">c:test</span>.<span class="title">txt</span> <span class="title">f</span>: &gt;<span class="title">nul</span> 2&gt;<span class="title">nul</span></span></span><br><span class="line"><span class="function">不显示”文件复制成功”的提示信息，并且<span class="title">f</span>盘不存在的话，也不显示错误提示信息</span></span><br><span class="line"><span class="function"><span class="title">echo</span> ^^<span class="title">W</span> ^&gt; ^<span class="title">W</span>&gt;<span class="title">c:test</span>.<span class="title">txt</span></span></span><br><span class="line"><span class="function">生成的文件内容为 ^<span class="title">W</span> &gt; <span class="title">W</span></span></span><br><span class="line"><span class="function">^ 和 &gt; 是控制命令，要把它们输出到文件，必须在前面加个 ^ 符号</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">27 &lt;</span></span><br><span class="line"><span class="function">从文件中获得输入信息，而不是从屏幕上</span></span><br><span class="line"><span class="function">一般用于 <span class="title">date</span> <span class="title">time</span> <span class="title">label</span> 等需要等待输入的命令</span></span><br><span class="line"><span class="function">@<span class="title">echo</span> <span class="title">off</span></span></span><br><span class="line"><span class="function"><span class="title">echo</span> 2005-05-01&gt;<span class="title">temp.txt</span></span></span><br><span class="line"><span class="function"><span class="title">date</span> &lt;<span class="title">temp.txt</span></span></span><br><span class="line"><span class="function"><span class="title">del</span> <span class="title">temp.txt</span></span></span><br><span class="line"><span class="function">这样就可以不等待输入直接修改当前日期</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">28 %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 %*</span></span><br><span class="line"><span class="function">命令行传递给批处理的参数</span></span><br><span class="line"><span class="function">%0 批处理文件本身</span></span><br><span class="line"><span class="function">%1 第一个参数</span></span><br><span class="line"><span class="function">%9 第九个参数</span></span><br><span class="line"><span class="function">%* 从第一个参数开始的所有参数</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">批参数(%<span class="title">n</span>)的替代已被增强。您可以使用以下语法:</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">%~1 - 删除引号(")，扩充 %1</span></span><br><span class="line"><span class="function">%~<span class="title">f1</span> - 将 %1 扩充到一个完全合格的路径名</span></span><br><span class="line"><span class="function">%~<span class="title">d1</span> - 仅将 %1 扩充到一个驱动器号</span></span><br><span class="line"><span class="function">%~<span class="title">p1</span> - 仅将 %1 扩充到一个路径</span></span><br><span class="line"><span class="function">%~<span class="title">n1</span> - 仅将 %1 扩充到一个文件名</span></span><br><span class="line"><span class="function">%~<span class="title">x1</span> - 仅将 %1 扩充到一个文件扩展名</span></span><br><span class="line"><span class="function">%~<span class="title">s1</span> - 扩充的路径指含有短名</span></span><br><span class="line"><span class="function">%~<span class="title">a1</span> - 将 %1 扩充到文件属性</span></span><br><span class="line"><span class="function">%~<span class="title">t1</span> - 将 %1 扩充到文件的日期/时间</span></span><br><span class="line"><span class="function">%~<span class="title">z1</span> - 将 %1 扩充到文件的大小</span></span><br><span class="line"><span class="function">%~$<span class="title">PATH</span> : 1 - 查找列在 <span class="title">PATH</span> 环境变量的目录，并将 %1</span></span><br><span class="line"><span class="function">扩充到找到的第一个完全合格的名称。如果环境</span></span><br><span class="line"><span class="function">变量名未被定义，或者没有找到文件，此组合键会</span></span><br><span class="line"><span class="function">扩充到空字符串</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">可以组合修定符来取得多重结果:</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">%~<span class="title">dp1</span> - 只将 %1 扩展到驱动器号和路径</span></span><br><span class="line"><span class="function">%~<span class="title">nx1</span> - 只将 %1 扩展到文件名和扩展名</span></span><br><span class="line"><span class="function">%~<span class="title">dp</span>$<span class="title">PATH</span>:1 - 在列在 <span class="title">PATH</span> 环境变量中的目录里查找 %1，</span></span><br><span class="line"><span class="function">并扩展到找到的第一个文件的驱动器号和路径。</span></span><br><span class="line"><span class="function">%~<span class="title">ftza1</span> - 将 %1 扩展到类似 <span class="title">DIR</span> 的输出行。</span></span><br><span class="line"><span class="function">可以参照 <span class="title">call</span>/? 或 <span class="title">for</span>/? 看出每个参数的含意</span></span><br><span class="line"><span class="function"><span class="title">echo</span> <span class="title">load</span> "%%1" "%%2"&gt;<span class="title">c:test</span>.<span class="title">txt</span></span></span><br><span class="line"><span class="function">生成的文件内容为 <span class="title">load</span> "%1" "%2"</span></span><br><span class="line"><span class="function">批处理文件里，用这个格式把命令行参数输出到文件</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">29 <span class="title">if</span></span></span><br><span class="line"><span class="function">判断命令</span></span><br><span class="line"><span class="function"><span class="title">if</span> "%1"=="/<span class="title">a</span>" <span class="title">echo</span> 第一个参数是/<span class="title">a</span></span></span><br><span class="line"><span class="function"><span class="title">if</span> /<span class="title">i</span> "%1" <span class="title">equ</span> "/<span class="title">a</span>" <span class="title">echo</span> 第一个参数是/<span class="title">a</span></span></span><br><span class="line"><span class="function">/<span class="title">i</span> 表示不区分大小写，<span class="title">equ</span> 和 == 是一样的，其它运算符参见 <span class="title">if</span>/?</span></span><br><span class="line"><span class="function"><span class="title">if</span> <span class="title">exist</span> <span class="title">c:test</span>.<span class="title">bat</span> <span class="title">echo</span> 存在<span class="title">c:test</span>.<span class="title">bat</span>文件</span></span><br><span class="line"><span class="function"><span class="title">if</span> <span class="title">not</span> <span class="title">exist</span> <span class="title">c:windows</span> (</span></span><br><span class="line"><span class="function"><span class="title">echo</span> 不存在<span class="title">c:windows</span>文件夹</span></span><br><span class="line"><span class="function">)</span></span><br><span class="line"><span class="function"><span class="title">if</span> <span class="title">exist</span> <span class="title">c:test</span>.<span class="title">bat</span> (</span></span><br><span class="line"><span class="function"><span class="title">echo</span> 存在<span class="title">c:test</span>.<span class="title">bat</span></span></span><br><span class="line"><span class="function">) <span class="title">else</span> (</span></span><br><span class="line"><span class="function"><span class="title">echo</span> 不存在<span class="title">c:test</span>.<span class="title">bat</span></span></span><br><span class="line"><span class="function">)</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">30 <span class="title">setlocal</span> 和 <span class="title">endlocal</span></span></span><br><span class="line"><span class="function">设置”命令扩展名”和”延缓环境变量扩充”</span></span><br><span class="line"><span class="function"><span class="title">SETLOCAL</span> <span class="title">ENABLEEXTENSIONS</span> #启用"命令扩展名"</span></span><br><span class="line"><span class="function"><span class="title">SETLOCAL</span> <span class="title">DISABLEEXTENSIONS</span> #停用"命令扩展名"</span></span><br><span class="line"><span class="function"><span class="title">SETLOCAL</span> <span class="title">ENABLEDELAYEDEXPANSION</span> #启用"延缓环境变量扩充"</span></span><br><span class="line"><span class="function"><span class="title">SETLOCAL</span> <span class="title">DISABLEDELAYEDEXPANSION</span> #停用"延缓环境变量扩充"</span></span><br><span class="line"><span class="function"><span class="title">ENDLOCAL</span> #恢复到使用<span class="title">SETLOCAL</span>语句以前的状态</span></span><br><span class="line"><span class="function">“命令扩展名”默认为启用</span></span><br><span class="line"><span class="function">“延缓环境变量扩充”默认为停用</span></span><br><span class="line"><span class="function">批处理结束系统会自动恢复默认值</span></span><br><span class="line"><span class="function">可以修改注册表以禁用"命令扩展名"，详见 <span class="title">cmd</span> /? 。所以用到"命令扩展名"的程</span></span><br><span class="line"><span class="function">序，建议在开头和结尾加上 <span class="title">SETLOCAL</span> <span class="title">ENABLEEXTENSIONS</span> 和 <span class="title">ENDLOCAL</span> 语句，以确</span></span><br><span class="line"><span class="function">保程序能在其它系统上正确运行</span></span><br><span class="line"><span class="function">"延缓环境变量扩充"主要用于 <span class="title">if</span> 和 <span class="title">for</span> 的符合语句，在 <span class="title">set</span> 的说明里有其实用例程</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">31 <span class="title">set</span></span></span><br><span class="line"><span class="function">设置变量</span></span><br><span class="line"><span class="function">引用变量可在变量名前后加 % ，即 %变量名%</span></span><br><span class="line"><span class="function"><span class="title">set</span> #显示目前所有可用的变量，包括系统变量和自定义的变量</span></span><br><span class="line"><span class="function"><span class="title">echo</span> %<span class="title">SystemDrive</span>% #显示系统盘盘符。系统变量可以直接引用</span></span><br><span class="line"><span class="function"><span class="title">set</span> <span class="title">p</span> #显示所有以<span class="title">p</span>开头的变量，要是一个也没有就设<span class="title">errorlevel</span>=1</span></span><br><span class="line"><span class="function"><span class="title">set</span> <span class="title">p</span>=<span class="title">aa1bb1aa2bb2</span> #设置变量<span class="title">p</span>，并赋值为 = 后面的字符串，即<span class="title">aa1bb1aa2bb2</span></span></span><br><span class="line"><span class="function"><span class="title">echo</span> %<span class="title">p</span>% #显示变量<span class="title">p</span>代表的字符串，即<span class="title">aa1bb1aa2bb2</span></span></span><br><span class="line"><span class="function"><span class="title">echo</span> %<span class="title">p</span>:~6% #显示变量<span class="title">p</span>中第6个字符以后的所有字符，即<span class="title">aa2bb2</span></span></span><br><span class="line"><span class="function"><span class="title">echo</span> %<span class="title">p</span>:~6,3% #显示第6个字符以后的3个字符，即<span class="title">aa2</span></span></span><br><span class="line"><span class="function"><span class="title">echo</span> %<span class="title">p</span>:~0,3% #显示前3个字符，即<span class="title">aa1</span></span></span><br><span class="line"><span class="function"><span class="title">echo</span> %<span class="title">p</span>:~-2% #显示最后面的2个字符，即<span class="title">b2</span></span></span><br><span class="line"><span class="function"><span class="title">echo</span> %<span class="title">p</span>:~0,-2% #显示除了最后2个字符以外的其它字符，即<span class="title">aa1bb1aa2b</span></span></span><br><span class="line"><span class="function"><span class="title">echo</span> %<span class="title">p:aa</span>=<span class="title">c</span>% #用<span class="title">c</span>替换变量<span class="title">p</span>中所有的<span class="title">aa</span>，即显示<span class="title">c1bb1c2bb2</span></span></span><br><span class="line"><span class="function"><span class="title">echo</span> %<span class="title">p:aa</span>=% #将变量<span class="title">p</span>中的所有<span class="title">aa</span>字符串置换为空，即显示1<span class="title">bb12bb2</span></span></span><br><span class="line"><span class="function"><span class="title">echo</span> %<span class="title">p</span>:*<span class="title">bb</span>=<span class="title">c</span>% #第一个<span class="title">bb</span>及其之前的所有字符被替换为<span class="title">c</span>，即显示<span class="title">c1aa2bb2</span></span></span><br><span class="line"><span class="function"><span class="title">set</span> <span class="title">p</span>=%<span class="title">p</span>:*<span class="title">bb</span>=<span class="title">c</span>% #设置变量<span class="title">p</span>，赋值为 %<span class="title">p</span>:*<span class="title">bb</span>=<span class="title">c</span>% ，即<span class="title">c1aa2bb2</span></span></span><br><span class="line"><span class="function"><span class="title">set</span> /<span class="title">a</span> <span class="title">p</span>=39 #设置<span class="title">p</span>为数值型变量，值为39</span></span><br><span class="line"><span class="function"><span class="title">set</span> /<span class="title">a</span> <span class="title">p</span>=39/10 #支持运算符，有小数时用去尾法，39/10=3.9，去尾得3，<span class="title">p</span>=3</span></span><br><span class="line"><span class="function"><span class="title">set</span> /<span class="title">a</span> <span class="title">p</span>=<span class="title">p</span>/10 #用 /<span class="title">a</span> 参数时，在 = 后面的变量可以不加%直接引用</span></span><br><span class="line"><span class="function"><span class="title">set</span> /<span class="title">a</span> <span class="title">p</span>=”1&amp;0″ #”与”运算，要加引号。其它支持的运算符参见<span class="title">set</span>/?</span></span><br><span class="line"><span class="function"><span class="title">set</span> <span class="title">p</span>= #取消<span class="title">p</span>变量</span></span><br><span class="line"><span class="function"><span class="title">set</span> /<span class="title">p</span> <span class="title">p</span>=请输入</span></span><br><span class="line"><span class="function">屏幕上显示”请输入”，并会将输入的字符串赋值给变量<span class="title">p</span></span></span><br><span class="line"><span class="function">注意这条可以用来取代 <span class="title">choice</span> 命令</span></span><br><span class="line"><span class="function">注意变量在 <span class="title">if</span> 和 <span class="title">for</span> 的复合语句里是一次性全部替换的，如</span></span><br><span class="line"><span class="function">@<span class="title">echo</span> <span class="title">off</span></span></span><br><span class="line"><span class="function"><span class="title">set</span> <span class="title">p</span>=<span class="title">aaa</span></span></span><br><span class="line"><span class="function"><span class="title">if</span> %<span class="title">p</span>%==<span class="title">aaa</span> (</span></span><br><span class="line"><span class="function"><span class="title">echo</span> %<span class="title">p</span>%</span></span><br><span class="line"><span class="function"><span class="title">set</span> <span class="title">p</span>=<span class="title">bbb</span></span></span><br><span class="line"><span class="function"><span class="title">echo</span> %<span class="title">p</span>%</span></span><br><span class="line"><span class="function">)</span></span><br><span class="line"><span class="function">结果将显示</span></span><br><span class="line"><span class="function"><span class="title">aaa</span></span></span><br><span class="line"><span class="function"><span class="title">aaa</span></span></span><br><span class="line"><span class="function">因为在读取 <span class="title">if</span> 语句时已经将所有 %<span class="title">p</span>% 替换为<span class="title">aaa</span></span></span><br><span class="line"><span class="function">这里的"替换"，在 /? 帮助里就是指"扩充"、"环境变量扩充"</span></span><br><span class="line"><span class="function">可以启用”延缓环境变量扩充”，用 ! 来引用变量，即 !变量名!</span></span><br><span class="line"><span class="function">@<span class="title">echo</span> <span class="title">off</span></span></span><br><span class="line"><span class="function"><span class="title">SETLOCAL</span> <span class="title">ENABLEDELAYEDEXPANSION</span></span></span><br><span class="line"><span class="function"><span class="title">set</span> <span class="title">p</span>=<span class="title">aaa</span></span></span><br><span class="line"><span class="function"><span class="title">if</span> %<span class="title">p</span>%==<span class="title">aaa</span> (</span></span><br><span class="line"><span class="function"><span class="title">echo</span> %<span class="title">p</span>%</span></span><br><span class="line"><span class="function"><span class="title">set</span> <span class="title">p</span>=<span class="title">bbb</span></span></span><br><span class="line"><span class="function"><span class="title">echo</span> !<span class="title">p</span>!</span></span><br><span class="line"><span class="function">)</span></span><br><span class="line"><span class="function"><span class="title">ENDLOCAL</span></span></span><br><span class="line"><span class="function">结果将显示</span></span><br><span class="line"><span class="function"><span class="title">aaa</span></span></span><br><span class="line"><span class="function"><span class="title">bbb</span></span></span><br><span class="line"><span class="function">还有几个动态变量，运行 <span class="title">set</span> 看不到</span></span><br><span class="line"><span class="function">%<span class="title">CD</span>% #代表当前目录的字符串</span></span><br><span class="line"><span class="function">%<span class="title">DATE</span>% #当前日期</span></span><br><span class="line"><span class="function">%<span class="title">TIME</span>% #当前时间</span></span><br><span class="line"><span class="function">%<span class="title">RANDOM</span>% #随机整数，介于0~32767</span></span><br><span class="line"><span class="function">%<span class="title">ERRORLEVEL</span>% #当前 <span class="title">ERRORLEVEL</span> 值</span></span><br><span class="line"><span class="function">%<span class="title">CMDEXTVERSION</span>% #当前命令处理器扩展名版本号</span></span><br><span class="line"><span class="function">%<span class="title">CMDCMDLINE</span>% #调用命令处理器的原始命令行</span></span><br><span class="line"><span class="function">可以用<span class="title">echo</span>命令查看每个变量值，如 <span class="title">echo</span> %<span class="title">time</span>%</span></span><br><span class="line"><span class="function">注意 %<span class="title">time</span>% 精确到毫秒，在批处理需要延时处理时可以用到</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">32 <span class="title">start</span></span></span><br><span class="line"><span class="function">批处理中调用外部程序的命令，否则等外部程序完成后才继续执行剩下的指令</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">33 <span class="title">call</span></span></span><br><span class="line"><span class="function">批处理中调用另外一个批处理的命令，否则剩下的批处理指令将不会被执行</span></span><br><span class="line"><span class="function">有时有的应用程序用<span class="title">start</span>调用出错的，也可以<span class="title">call</span>调用</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">34 <span class="title">choice</span> (外部命令)</span></span><br><span class="line"><span class="function">选择命令</span></span><br><span class="line"><span class="function">让用户输入一个字符，从而选择运行不同的命令，返回码<span class="title">errorlevel</span>为1234……</span></span><br><span class="line"><span class="function"><span class="title">win98</span>里是<span class="title">http</span>://<span class="title">choice.com</span></span></span><br><span class="line"><span class="function"><span class="title">win2000pro</span>里没有，可以从<span class="title">win98</span>里拷过来</span></span><br><span class="line"><span class="function"><span class="title">win2003</span>里是<span class="title">choice.exe</span></span></span><br><span class="line"><span class="function"><span class="title">choice</span> /<span class="title">N</span> /<span class="title">C</span> <span class="title">y</span> /<span class="title">T</span> 5 /<span class="title">D</span> <span class="title">y</span>&gt;<span class="title">nul</span></span></span><br><span class="line"><span class="function">延时5秒</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">35 <span class="title">assoc</span> 和 <span class="title">ftype</span></span></span><br><span class="line"><span class="function">文件关联</span></span><br><span class="line"><span class="function"><span class="title">assoc</span> 设置'文件扩展名'关联，关联到'文件类型'</span></span><br><span class="line"><span class="function"><span class="title">ftype</span> 设置'文件类型'关联，关联到'执行程序和参数'</span></span><br><span class="line"><span class="function">当你双击一个.<span class="title">txt</span>文件时，<span class="title">windows</span>并不是根据.<span class="title">txt</span>直接判断用 <span class="title">notepad.exe</span> 打开</span></span><br><span class="line"><span class="function">而是先判断.<span class="title">txt</span>属于 <span class="title">txtfile</span> '文件类型'</span></span><br><span class="line"><span class="function">再调用 <span class="title">txtfile</span> 关联的命令行 <span class="title">txtfile</span>=%<span class="title">SystemRoot</span>%<span class="title">system32NOTEPAD.EXE</span> %1</span></span><br><span class="line"><span class="function">可以在"文件夹选项"→"文件类型"里修改这2种关联</span></span><br><span class="line"><span class="function"><span class="title">assoc</span> #显示所有'文件扩展名'关联</span></span><br><span class="line"><span class="function"><span class="title">assoc</span> .<span class="title">txt</span> #显示.<span class="title">txt</span>代表的'文件类型'，结果显示 .<span class="title">txt</span>=<span class="title">txtfile</span></span></span><br><span class="line"><span class="function"><span class="title">assoc</span> .<span class="title">doc</span> #显示.<span class="title">doc</span>代表的'文件类型'，结果显示 .<span class="title">doc</span>=<span class="title">Word.Document</span>.8</span></span><br><span class="line"><span class="function"><span class="title">assoc</span> .<span class="title">exe</span> #显示.<span class="title">exe</span>代表的'文件类型'，结果显示 .<span class="title">exe</span>=<span class="title">exefile</span></span></span><br><span class="line"><span class="function"><span class="title">ftype</span> #显示所有'文件类型'关联</span></span><br><span class="line"><span class="function"><span class="title">ftype</span> <span class="title">exefile</span> #显示<span class="title">exefile</span>类型关联的命令行，结果显示 <span class="title">exefile</span>="%1" %*</span></span><br><span class="line"><span class="function"><span class="title">assoc</span> .<span class="title">txt</span>=<span class="title">Word.Document</span>.8</span></span><br><span class="line"><span class="function">设置.<span class="title">txt</span>为<span class="title">word</span>类型的文档，可以看到.<span class="title">txt</span>文件的图标都变了</span></span><br><span class="line"><span class="function"><span class="title">assoc</span> .<span class="title">txt</span>=<span class="title">txtfile</span></span></span><br><span class="line"><span class="function">恢复.<span class="title">txt</span>的正确关联</span></span><br><span class="line"><span class="function"><span class="title">ftype</span> <span class="title">exefile</span>="%1" %*</span></span><br><span class="line"><span class="function">恢复 <span class="title">exefile</span> 的正确关联</span></span><br><span class="line"><span class="function">如果该关联已经被破坏，可以运行 <span class="title">http</span>://<span class="title">command.com</span> ，再输入这条命令</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">36 <span class="title">pushd</span> 和 <span class="title">popd</span></span></span><br><span class="line"><span class="function">切换当前目录</span></span><br><span class="line"><span class="function">@<span class="title">echo</span> <span class="title">off</span></span></span><br><span class="line"><span class="function"><span class="title">c</span>: &amp; <span class="title">cd</span> &amp; <span class="title">md</span> <span class="title">mp3</span> #在 <span class="title">C</span>: 建立 <span class="title">mp3</span> 文件夹</span></span><br><span class="line"><span class="function"><span class="title">md</span> <span class="title">d:mp4</span> #在 <span class="title">D</span>: 建立 <span class="title">mp4</span> 文件夹</span></span><br><span class="line"><span class="function"><span class="title">cd</span> /<span class="title">d</span> <span class="title">d:mp4</span> #更改当前目录为 <span class="title">d:mp4</span></span></span><br><span class="line"><span class="function"><span class="title">pushd</span> <span class="title">c:mp3</span> #保存当前目录，并切换当前目录为 <span class="title">c:mp3</span></span></span><br><span class="line"><span class="function"><span class="title">popd</span> #恢复当前目录为刚才保存的 <span class="title">d:mp4</span></span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">37 <span class="title">for</span></span></span><br><span class="line"><span class="function">循环命令</span></span><br><span class="line"><span class="function">这个比较复杂，请对照 <span class="title">for</span>/? 来看</span></span><br><span class="line"><span class="function"><span class="title">for</span> %%<span class="title">i</span> <span class="title">in</span> (<span class="title">c</span>: <span class="title">d</span>: <span class="title">e</span>: <span class="title">f</span>:) <span class="title">do</span> <span class="title">echo</span> %%<span class="title">i</span></span></span><br><span class="line"><span class="function">依次调用小括号里的每个字符串，执行 <span class="title">do</span> 后面的命令</span></span><br><span class="line"><span class="function">注意%%<span class="title">i</span>，在批处理中 <span class="title">for</span> 语句调用参数用2个%</span></span><br><span class="line"><span class="function">默认的字符串分隔符是"空格键"，"<span class="title">Tab</span>键"，"回车键"</span></span><br><span class="line"><span class="function"><span class="title">for</span> %%<span class="title">i</span> <span class="title">in</span> (*.<span class="title">txt</span>) <span class="title">do</span> <span class="title">find</span> "<span class="title">abc</span>" %%<span class="title">i</span></span></span><br><span class="line"><span class="function">对当前目录里所有的<span class="title">txt</span>文件执行 <span class="title">find</span> 命令</span></span><br><span class="line"><span class="function"><span class="title">for</span> /<span class="title">r</span> . %%<span class="title">i</span> <span class="title">in</span> (*.<span class="title">txt</span>) <span class="title">do</span> <span class="title">find</span> "<span class="title">abc</span>" %%<span class="title">i</span></span></span><br><span class="line"><span class="function">在当前目录和子目录里所有的.<span class="title">txt</span>文件中搜索包含 <span class="title">abc</span> 字符串的行</span></span><br><span class="line"><span class="function"><span class="title">for</span> /<span class="title">r</span> . %%<span class="title">i</span> <span class="title">in</span> (.) <span class="title">do</span> <span class="title">echo</span> %%~<span class="title">pni</span></span></span><br><span class="line"><span class="function">显示当前目录名和所有子目录名，包括路径，不包括盘符</span></span><br><span class="line"><span class="function"><span class="title">for</span> /<span class="title">r</span> <span class="title">d:mp3</span> %%<span class="title">i</span> <span class="title">in</span> (*.<span class="title">mp3</span>) <span class="title">do</span> <span class="title">echo</span> %%<span class="title">i</span>&gt;&gt;<span class="title">d:mp3</span>.<span class="title">txt</span></span></span><br><span class="line"><span class="function">把 <span class="title">d:mp3</span> 及其子目录里的<span class="title">mp3</span>文件的文件名都存到 <span class="title">d:mp3</span>.<span class="title">txt</span> 里去</span></span><br><span class="line"><span class="function"><span class="title">for</span> /<span class="title">l</span> %%<span class="title">i</span> <span class="title">in</span> (2,1,8) <span class="title">do</span> <span class="title">echo</span> %%<span class="title">i</span></span></span><br><span class="line"><span class="function">生成2345678的一串数字，2是数字序列的开头，8是结尾，1表示每次加1</span></span><br><span class="line"><span class="function"><span class="title">for</span> /<span class="title">f</span> %%<span class="title">i</span> <span class="title">in</span> ('<span class="title">set</span>') <span class="title">do</span> <span class="title">echo</span> %%<span class="title">i</span></span></span><br><span class="line"><span class="function">对 <span class="title">set</span> 命令的输出结果循环调用，每行一个</span></span><br><span class="line"><span class="function"><span class="title">for</span> /<span class="title">f</span> "<span class="title">eol</span>=<span class="title">P</span>" %%<span class="title">i</span> <span class="title">in</span> ('<span class="title">set</span>') <span class="title">do</span> <span class="title">echo</span> %%<span class="title">i</span></span></span><br><span class="line"><span class="function">取 <span class="title">set</span> 命令的输出结果，忽略以 <span class="title">P</span> 开头的那几行</span></span><br><span class="line"><span class="function"><span class="title">for</span> /<span class="title">f</span> %%<span class="title">i</span> <span class="title">in</span> (<span class="title">d:mp3</span>.<span class="title">txt</span>) <span class="title">do</span> <span class="title">echo</span> %%<span class="title">i</span></span></span><br><span class="line"><span class="function">显示 <span class="title">d:mp3</span>.<span class="title">txt</span> 里的每个文件名，每行一个，不支持带空格的名称</span></span><br><span class="line"><span class="function"><span class="title">for</span> /<span class="title">f</span> "<span class="title">delims</span>=" %%<span class="title">i</span> <span class="title">in</span> (<span class="title">d:mp3</span>.<span class="title">txt</span>) <span class="title">do</span> <span class="title">echo</span> %%<span class="title">i</span></span></span><br><span class="line"><span class="function">显示 <span class="title">d:mp3</span>.<span class="title">txt</span> 里的每个文件名，每行一个，支持带空格的名称</span></span><br><span class="line"><span class="function"><span class="title">for</span> /<span class="title">f</span> "<span class="title">skip</span>=5 <span class="title">tokens</span>=4" %%<span class="title">a</span> <span class="title">in</span> ('<span class="title">dir</span>') <span class="title">do</span> <span class="title">echo</span> %%<span class="title">a</span></span></span><br><span class="line"><span class="function">对 <span class="title">dir</span> 命令的结果，跳过前面5行，余下的每行取第4列</span></span><br><span class="line"><span class="function">每列之间的分隔符为默认的"空格"</span></span><br><span class="line"><span class="function">可以注意到 <span class="title">dir</span> 命令输出的前5行是没有文件名的</span></span><br><span class="line"><span class="function"><span class="title">for</span> /<span class="title">f</span> "<span class="title">tokens</span>=1,2,3 <span class="title">delims</span>=- " %%<span class="title">a</span> <span class="title">in</span> ('<span class="title">date</span> /<span class="title">t</span>') <span class="title">do</span> (</span></span><br><span class="line"><span class="function"><span class="title">echo</span> %%<span class="title">a</span></span></span><br><span class="line"><span class="function"><span class="title">echo</span> %%<span class="title">b</span></span></span><br><span class="line"><span class="function"><span class="title">echo</span> %%<span class="title">c</span></span></span><br><span class="line"><span class="function">)</span></span><br><span class="line"><span class="function">对 <span class="title">date</span> /<span class="title">t</span> 的输出结果，每行取1、2、3列</span></span><br><span class="line"><span class="function">第一列对应指定的 %%<span class="title">a</span> ，后面的 %%<span class="title">b</span> 和 %%<span class="title">c</span> 是派生出来的，对应其它列</span></span><br><span class="line"><span class="function">分隔符指定为 - 和"空格"，注意 <span class="title">delims</span>=- 后面有个"空格"</span></span><br><span class="line"><span class="function">其中 <span class="title">tokens</span>=1,2,3 若用 <span class="title">tokens</span>=1-3 替换，效果是一样的</span></span><br><span class="line"><span class="function"><span class="title">for</span> /<span class="title">f</span> "<span class="title">tokens</span>=2* <span class="title">delims</span>=- " %%<span class="title">a</span> <span class="title">in</span> ('<span class="title">date</span> /<span class="title">t</span>') <span class="title">do</span> <span class="title">echo</span> %%<span class="title">b</span></span></span><br><span class="line"><span class="function">取第2列给 %%<span class="title">a</span> ，其后的列都给 %%<span class="title">b</span></span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">38 <span class="title">subst</span> (外部命令)</span></span><br><span class="line"><span class="function">映射磁盘。</span></span><br><span class="line"><span class="function"><span class="title">subst</span> <span class="title">z</span>: <span class="title">serverd</span> #这样输入<span class="title">z</span>:就可以访问<span class="title">serverd</span>了</span></span><br><span class="line"><span class="function"><span class="title">subst</span> <span class="title">z</span>: /<span class="title">d</span> #取消该映射</span></span><br><span class="line"><span class="function"><span class="title">subst</span> #显示目前所有的映时</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">39 <span class="title">xcopy</span> (外部命令)</span></span><br><span class="line"><span class="function">文件拷贝</span></span><br><span class="line"><span class="function"><span class="title">xcopy</span> <span class="title">d:mp3</span> <span class="title">e:mp3</span> /<span class="title">s</span>/<span class="title">e</span>/<span class="title">i</span>/<span class="title">y</span></span></span><br><span class="line"><span class="function">复制 <span class="title">d:mp3</span> 文件夹、所有子文件夹和文件到 <span class="title">e</span>: ，覆盖已有文件</span></span><br><span class="line"><span class="function">加 /<span class="title">i</span> 表示如果 <span class="title">e</span>: 没有 <span class="title">mp3</span> 文件夹就自动新建一个，否则会有询问</span></span><br></pre></td></tr></table></figure>
      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/bat/" rel="tag"># bat</a>
          
            <a href="/tags/批处理/" rel="tag"># 批处理</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/12/24/windows下部署hexo/" rel="next" title="Windows下部署hexo">
                <i class="fa fa-chevron-left"></i> Windows下部署hexo
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/01/09/bat批处理自动创建并打开博客文章/" rel="prev" title="bat批处理自动创建并打开博客文章">
                bat批处理自动创建并打开博客文章 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">xsin</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">29</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">9</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">25</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://github.com/x51n" title="GitHub &rarr; https://github.com/x51n" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="mailto:xsintan@163.com" title="E-Mail &rarr; mailto:xsintan@163.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            </div>
          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#在Windows中善用批处理可以简化很多重复工作"><span class="nav-number">1.</span> <span class="nav-text">在Windows中善用批处理可以简化很多重复工作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">2.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#更新记录"><span class="nav-number">3.</span> <span class="nav-text">更新记录</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#什么是批处理"><span class="nav-number">4.</span> <span class="nav-text">什么是批处理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#批处理命令简介"><span class="nav-number">5.</span> <span class="nav-text">批处理命令简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#批处理符号简介"><span class="nav-number">6.</span> <span class="nav-text">批处理符号简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#常用DOS命令"><span class="nav-number">7.</span> <span class="nav-text">常用DOS命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Windows-Batch-常用命令"><span class="nav-number">8.</span> <span class="nav-text">Windows Batch 常用命令</span></a></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">xsin</span>

  

  
</div>


  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v7.0.1</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
























  



  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script src="/lib/reading_progress/reading_progress.js"></script>


  


  <script src="/js/src/utils.js?v=7.0.1"></script>

  <script src="/js/src/motion.js?v=7.0.1"></script>



  
  


  <script src="/js/src/affix.js?v=7.0.1"></script>

  <script src="/js/src/schemes/pisces.js?v=7.0.1"></script>




  
  <script src="/js/src/scrollspy.js?v=7.0.1"></script>
<script src="/js/src/post-details.js?v=7.0.1"></script>



  


  <script src="/js/src/bootstrap.js?v=7.0.1"></script>


  
  
  
  <script id="dsq-count-scr" src="https://xsin.disqus.com/count.js" async></script>


<script>
  var disqus_config = function() {
    this.page.url = "https://xsin.gitee.io/2018/12/25/Windows批处理(cmdbat)常用命令小结/";
    this.page.identifier = "2018/12/25/Windows批处理(cmdbat)常用命令小结/";
    this.page.title = 'Windows批处理(cmd/bat)常用命令小结';
    };
  function loadComments() {
    var d = document, s = d.createElement('script');
    s.src = 'https://xsin.disqus.com/embed.js';
    s.setAttribute('data-timestamp', '' + +new Date());
    (d.head || d.body).appendChild(s);
  }
  
    loadComments();
  
</script>





  


  
  <script>
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  

  

  

  

  

  

  

  

  

  

<script src="/live2dw/lib/L2Dwidget.min.js?0c58a1486de42ac6cc1c59c7d98ae887"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/wanko.model.json"},"display":{"position":"right","width":75,"height":150},"mobile":{"show":true}});</script><!-- hexo-inject:begin --><!-- Begin: Injected MathJax -->
<script type="text/x-mathjax-config">
  MathJax.Hub.Config("");
</script>

<script type="text/x-mathjax-config">
  MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
      all[i].SourceElement().parentNode.className += ' has-jax';
    }
  });
</script>

<script type="text/javascript" src="">
</script>
<!-- End: Injected MathJax -->
<!-- hexo-inject:end --></body>
</html>
